// Wire Client Program for PCMod (SERVER)

PROG.PName = "WireClient"
PROG.Author = "[GU]|ZLC|thomasfn"

PROG.InputCount = 5

function PROG:Initialize( pl )
	SendClientProgram( pl, "WireClient" )
	SendClientWindow( pl, "Main" )
	
	OpenPort( pl, 24 )
	
	self:PC_Changed( pl )
end

function PROG:Unload()
	
end

function PROG:Think()

end

function PROG:CommandRecieved( ply, command, args )
	if (command == "send_output") then
		result = ComposePacket( ply, args[1], 25, { "wire_setoutput", args[2], args[3] } )
		PData[pc:EntIndex()].WireLastIP = args[1]
	end
	if (command == "get_inputs") then
		result = ComposePacket( ply, args[1], 25, { "wire_getinputs" } )
		PData[pc:EntIndex()].WireLastIP = args[1]
	end
	if (command == "updateme") then
		if (!PData[pc:EntIndex()].WireLastIP) then PData[pc:EntIndex()].WireLastIP = "192.168.0.1" end
		BeamData_String( ply, "lastip", PData[pc:EntIndex()].WireLastIP )
		BeamData_Int( ply, "outputcount", self.OutputCount )
	end
	if (command == "quit") then
		UnloadProgram( ply )
	end
end

function PROG:PC_Changed( ply )
	pc = ply.CurrentPC
	if (!pc) then return end
	PData[pc:EntIndex()].LPlayer = ply
end

function PROG:PacketRecieved( packet, pc )
	if (!pc) then return end
	ply = PData[pc:EntIndex()].LPlayer
	if (packet[4][1] == "wireinputs") then
		// We have wire inputs, do something with them
		if (!packet[4][2]) then return end
		local inputs = {}
		for i=1,self.InputCount do
			local key = tostring(i)
			local val = packet[4][2][ key ]
			if (!val) then val = 0 end
			inputs[ i ] = tostring(val)
		end
		local txt = table.concat( inputs, "+" )
		BeamData_String( ply, "inputs", txt )
	end
end